package com.athuiju.fenxiaoback.service.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.athuiju.fenxiaoback.bean.AlipayBean;
import com.athuiju.fenxiaoback.config.AlipayUtil;
import com.athuiju.fenxiaoback.entity.OmsOrder;
import com.athuiju.fenxiaoback.service.ALPayService;
import com.athuiju.fenxiaoback.service.OmsOrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.Random;


@Service(value = "alipayOrderService")
public class ALPayServiceImpl implements ALPayService {

    @Autowired
    private OmsOrderService orderService;

    @Override
    public Byte checkAlipay(String outTradeNo) {


        return 0;

    }



    @Override
    public String aliPay(String orderNo) throws AlipayApiException {
        //1 根据订单号查询订单信息
        QueryWrapper<OmsOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("order_sn", orderNo);
        OmsOrder order = orderService.getOne(wrapper);

        AlipayBean alipayBean = new AlipayBean();
        //alipayBean.setBody(order.getCourseTitle());
        alipayBean.setBody("易高家具全屋定制");
        alipayBean.setOut_trade_no(order.getOrderSn());


        String format = new DecimalFormat("#.00").format(order.getPayAmount());
        alipayBean.setTotal_amount(new StringBuffer(format));



        //alipayBean.setTotal_amount(new StringBuffer().append(order.getPayAmount()));

        System.out.println("%%%%%%%%%"+new StringBuffer().append( order.getPayAmount()+""));
        alipayBean.setSubject("订单名称：瞎写的"+ new Random().toString().substring(1,5));
        //.setTotal_amount(new StringBuffer().append(total_amount)).setSubject(subject);

        String alurl= AlipayUtil.connect(alipayBean);
        //alurl=alurl.replaceAll("&quot;","'");
        System.out.println("================"+alurl+"=============");
        return alurl; //AlipayBean alipayBean
    }


}
